<?php
namespace Home\Controller;
use Think\Controller;
class YuyueController extends HomeController
{
    public function add()
    {
    	if(IS_POST)
    	{
    		$model = D('Admin/Yuyue');

            $timedate = M('CourseCalendar')->where(array("id"=>I('id')))->getField("course_date");
            if((strtotime($timedate)-time())>(86400*2))
            {
                $this->error("只能预约2天内的课程");
            }
           $_POST['company_id'] = (int)session('admin.company_id');
           $_POST['course_id']  = M('CourseCalendar')->where(array("id"=>I('id')))->getField("course_id");

            if($res=$model->create(I('post.'), 1))
    		{
                $where['telphone'] = I('yueyu_tel');
                $res = M('member')->where($where)->find();

                $getg = getcheckcardguize($res['id'],I('course_id'));
                if($getg)
                {
                    $this->error("无法预约");
                }


                $checkguize =getsetcardguize($res['id'],$where['telphone'],$where['company_id']);
                if($checkguize['status']!='3')
                {
                    $this->error($checkguize['info']);
                }


                if($id = $model->add())
                {
//                    echo $model->_sql();
//                    die;
                    //
                    $this->success('预约成功！');
                    exit;
                }


    		}
    		$this->error($model->getError());
    	}

		// // 设置页面中的信息
		// $this->assign(array(
		// 	'_page_title' => '添加瑜伽馆预约',
		// 	'_page_btn_name' => '瑜伽馆预约列表',
		// 	'_page_btn_link' => U('lst'),
		// ));
		// $this->display();
    }
    public function index()
    {
      if(I('time'))
      {
        $time = I('time');
      }else {
        $time = date("Y-m-d",time());
      }

    	$model = D('CourseCalendar');
      $where['course_date'] = array("like","%".$time."%");
    	$data = $model->where($where)->order("course_date asc")->select();

    	$this->data = $data;

		// 设置页面中的信息
        $this->urlcode = urlencode("http://".$_SERVER['HTTP_HOST'].U('Yuyue/index'));
    	$this->display();
    }

	/**
	 * @param weekindex 周表
	 * @name 直接根据当前周几 判断出当前一周
	 */
	public function weekindex()
	{
    $model = D('CourseCalendar');
    if(I('time'))
    {
        $time =   getallweek(I('time'));
        foreach ($time as $key => $value) {
          $str =" is_show = '1' ".$value['str'];
          $res[$value['time']] = $model->where($str)->order("course_date asc")->select();
        }

    }else{
        $time =   getallweek(date("Y-m-d",time()));
        foreach ($time as $key => $value) {
          $str =" is_show = '1' ".$value['str'];
          $res[$value['time']] = $model->where($str)->order("course_date asc")->select();
        }
    }



   $this->res = $res;
        $this->urlcode = urlencode("http://".$_SERVER['HTTP_HOST'].U('Yuyue/weekindex'));
		$this->display();
	}

    public function quxiao()
    {
      if(IS_POST)
      {

        $this->res = M("yuyue")->where(array("yueyu_tel"=>I('mobile'),'status'=>'0'))->select();

      }
      $this->display();
      # code...
    }

    public function ajaxquxiao()
    {
        $id = I('id');
        $res = M("yuyue")->where(array("id"=>$id))->save(array("status"=>1));
        die(json_encode($res));
    }

    public function detail()
    {
        $id = I('get.id');
        $data = D('Course')->where([
            'id'=>['eq',$id]
        ])->find();
        $this->assign('data',$data);
        $this->display();
    }
}
